<?php

/**
 * Beaver_Model
 * 
 * @category   Beaver
 * @package    Beaver
 * @uses       Beaver_Model_Abstract
 * @author     Mihály Kuprivecz
 */
class Beaver_Model extends Beaver_Model_Abstract {
	
	
	public $validateBeforeSave = true;
	
	/**
	 * Call the parent constructor
	 *
	 * @param array $attributes
	 * @param string $mapperClass
	 * @return void
	 */
	public function __construct(array $attributes = null, $mapperClass = null) {
		parent::__construct($attributes, $mapperClass);
	}
	
	/**
	 * Find an entry
	 *
	 * Resets entry state if matching id found.
	 * 
	 * @param  int $id 
	 * @return Beaver_Model
	 */
	public function find( $id = null ) {
		$result = $this->getMapper ()->find ( $id );
		if (is_array ( $result )) {
			return $result;
		}
		return $this;
	}
	
	
	public function findAll($filter = null, $order = null, $count = null, $offset = null) {
		$result = $this->getMapper ()->findAll($filter, $order, $count, $offset);
		return $result;
	}
	
	public function findCount($where = null)
	{
		return $this->getMapper ()->findCount($where);
	}
	
	/**
	 * Save an entry from the model
	 *
	 * Resets entry state if matching id found.
	 * 
	 * @param  int $id 
	 * @return Beaver_Model
	 */
	public function save() {
		if ($this->validateBeforeSave)
		{
			$valid = $this->validate();
			if (true === $this->validate())
			{
				return $this->getMapper ()->save ();
			} else {
				return $valid;	
			}
		}
		$result = $this->getMapper ()->save ();
	}
	
	/**
	 * Save an entry from the model
	 *
	 * Resets entry state if matching id found.
	 * 
	 * @param  int $id 
	 * @return Beaver_Model
	 */
	public function delete( $id = null ) {
		return $this->getMapper ()->delete ($id);
	}
	
	

}